//
//  ColorPickerView.m
//  FMSManager
//
//  Created by hongbao.cui on 14-12-10.
//  Copyright (c) 2014年 hongbao.cui. All rights reserved.
//

#import "ColorPickerView.h"
#import "KZColorPicker.h"
#import "ApplicationUtils.h"
@implementation ColorPickerView
@synthesize delegate;
@synthesize selectedColor;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)back{
    CGRect rect = [UIScreen mainScreen].bounds;
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, rect.size.height, self.frame.size.width, self.frame.size.height);
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}
-(void)submmitBtnClicked:(id)sender{
    CGRect rect = [UIScreen mainScreen].bounds;
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, rect.size.height, self.frame.size.width, self.frame.size.height);
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
    [delegate ColorPickerView:self didChangeColor:self.selectedColor];
}
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        CGRect rect = [UIScreen mainScreen].bounds;
        UIImageView *headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 64)];
        [headerView setImage:[[ApplicationUtils getLocalImage:@"Navigation-Bar_backgroud_44@2x"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]];
        [headerView setUserInteractionEnabled:YES];
        [self addSubview:headerView];
        //返回按钮
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setFrame:CGRectMake(5.0, 20+10.5, 23.0, 23.0)];
        [backBtn setBackgroundImage:[ApplicationUtils getLocalImage:@"Navigation-Bar_Return_btn_@2x"] forState:UIControlStateNormal];
        [backBtn setBackgroundImage:[ApplicationUtils getLocalImage:@"Navigation-Bar_Return_btn_@2x"] forState:UIControlStateHighlighted];
        [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:backBtn];
        
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((rect.size.width-100)/2, 20, 100, 44)];
        titleLabel.text = @"颜色选择器";
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:titleLabel];
        
        KZColorPicker *picker = [[KZColorPicker alloc] initWithFrame:CGRectMake(0, 64, rect.size.width, rect.size.height-64)];
        picker.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        picker.selectedColor = self.selectedColor;
        picker.oldColor = self.selectedColor;
        [picker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:picker];
        
        UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [setBtn setFrame:CGRectMake(FMSCREENWIDTH-33.0, 20+10.5, 23.0, 23.0)];
        [setBtn setTitle:@"确定" forState:UIControlStateNormal];
        [setBtn setTitle:@"确定" forState:UIControlStateHighlighted];
        [setBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [setBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
//        [setBtn setBackgroundImage:[ApplicationUtils getLocalImage:@"person_icon5@2x"] forState:UIControlStateNormal];
//        [setBtn setBackgroundImage:[ApplicationUtils getLocalImage:@"person_icon5@2x"] forState:UIControlStateHighlighted];
        [setBtn addTarget:self action:@selector(submmitBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:setBtn];
    }
    return self;
}

- (void) pickerChanged:(KZColorPicker *)cp
{
    self.selectedColor = cp.selectedColor;
}
-(void)pushToView{
    CGRect rect = [UIScreen mainScreen].bounds;
    self.frame = CGRectMake(0, rect.size.height, self.frame.size.width, self.frame.size.height);
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(rect.origin.x, rect.origin.y, self.frame.size.width, self.frame.size.height);
    }];
}
@end
